package middol.base

import grails.converters.JSON
import grails.plugin.springsecurity.SpringSecurityService
import grails.util.Holders
import groovy.util.logging.Slf4j
import org.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor
import org.hibernate.event.spi.PostInsertEvent
import org.hibernate.event.spi.PostUpdateEvent
import org.hibernate.event.spi.PreDeleteEvent
import org.springframework.beans.factory.annotation.Autowired

/**
* @Description:    自定义操作日志
* @Author:         zhangjiayu@middol.com
* @CreateDate:     2020/7/21 22:27
* @Version:        1.0
*/
@Slf4j
class AuditEventListener extends ClosureEventTriggeringInterceptor{
    @Autowired
    AuditLogService auditLogService
//    AuditLogService auditLogService = Holders.grailsApplication.mainContext.getBean(AuditLogService.class)
//    AuditLogService auditLogService = new AuditLogService()
    @Override
    void onPostInsert(PostInsertEvent event) {
        auditLogService.saveAuditLog("INSERT", event?.entity)
    }

    @Override
    boolean onPreDelete(PreDeleteEvent event) {
        auditLogService.saveAuditLog("DELETE", event?.entity)
        return false
    }

    @Override
    void onPostUpdate(PostUpdateEvent event) {
        auditLogService.saveAuditLog("UPDATE", event?.entity)
    }
}
